package com.hospital.diagnosis.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.enums.diagnosis.module.AIAcceptType;
import com.hospital.common.pojo.TAiDiagnosisRecord;
import com.hospital.common.vo.diagnosis.module.AiDiagnosisRecordVo;
import com.hospital.diagnosis.module.exception.TAiDiagnosisRecordServiceException;
import com.hospital.diagnosis.module.exception.enums.TAiDiagnosisRecordServiceExpCode;
import com.hospital.diagnosis.module.mapper.TAiDiagnosisRecordMapper;
import com.hospital.diagnosis.module.service.TAiDiagnosisRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class TAiDiagnosisRecordServiceImpl extends ServiceImpl<TAiDiagnosisRecordMapper, TAiDiagnosisRecord> implements TAiDiagnosisRecordService {
    @Autowired
    private TAiDiagnosisRecordMapper aiDiagnosisRecordMapper;

    @Override
    public List<AiDiagnosisRecordVo> queryByPatientId(Long patientId) {
        return aiDiagnosisRecordMapper.selectList(new LambdaQueryWrapper<>(TAiDiagnosisRecord.class).eq(TAiDiagnosisRecord::getPatientId,patientId))
                .stream().map(this::convert).toList();
    }

    @Override
    public void accept(Long patientId, Long diagnosisId) {
        LambdaQueryWrapper<TAiDiagnosisRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TAiDiagnosisRecord::getPatientId, patientId)
                .eq(TAiDiagnosisRecord::getId, diagnosisId);
        TAiDiagnosisRecord diagnosisRecord = aiDiagnosisRecordMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(diagnosisRecord)){
            throw new TAiDiagnosisRecordServiceException(TAiDiagnosisRecordServiceExpCode.DIAGNOSIS_NOT_EXIST);
        }
        if(diagnosisRecord.getAccept().equals(AIAcceptType.ACCEPT.value)){
            throw new TAiDiagnosisRecordServiceException(TAiDiagnosisRecordServiceExpCode.DIAGNOSIS_ALREADY_ACCEPTED);
        }
        LambdaUpdateWrapper<TAiDiagnosisRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TAiDiagnosisRecord::getPatientId, patientId)
                .eq(TAiDiagnosisRecord::getId, diagnosisId)
                .set(TAiDiagnosisRecord::getAccept, AIAcceptType.ACCEPT.value);
        if (aiDiagnosisRecordMapper.update(updateWrapper)==0){
            throw new TAiDiagnosisRecordServiceException(TAiDiagnosisRecordServiceExpCode.ACCEPT_FAILED);
        }
    }
    private AiDiagnosisRecordVo convert(TAiDiagnosisRecord diagnosisRecord) {
        AiDiagnosisRecordVo diagnosisRecordVo = new AiDiagnosisRecordVo();
        BeanUtils.copyProperties(diagnosisRecord, diagnosisRecordVo);
        return diagnosisRecordVo;
    }
}
